package com.estar.assistsurvy.activity;

import java.util.ArrayList;
import java.util.List;

import com.estar.assistsurvy.R;
import com.estar.assistsurvy.adapter.ChargeListAdapter;
import com.estar.assistsurvy.adapter.EvaluateListAdapter;
import com.estar.assistsurvy.entity.CaseEvaluationInfo;
import com.estar.assistsurvy.entity.ChargeVO;
import com.estar.assistsurvy.entity.Evaluation;
import com.estar.assistsurvy.entity.HistoricalCase;
import com.estar.assistsurvy.widget.NotScrollListview;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TaskDetailActivity extends BaseActivity implements OnClickListener{

	private LinearLayout returnBack,evaluate,totalCharge;
	private NotScrollListview evaluate_list,charge_list;
	private TextView chargeNum,caseNumber,carNumber,customPhone,startPoint,endPoint,distance;
	private boolean evaluateVisibal=false,chargeVisibal=false;
	private Intent intent;
	private HistoricalCase historicalCase;
	private CaseEvaluationInfo caseEvaluation;
	private List<Evaluation> evaluates;
	private List<ChargeVO> chargeList;
	private EvaluateListAdapter evaAdapter;
	private ChargeListAdapter charAdapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tastdetail);
		initView();
	}
	
	private void initView(){
		returnBack=find(R.id.returnBack);
		returnBack.setOnClickListener(this);
		evaluate=find(R.id.evaluate);
		evaluate.setOnClickListener(this);
		totalCharge=find(R.id.totalCharge);
		totalCharge.setOnClickListener(this);
		caseNumber=find(R.id.caseNumber);
		carNumber=find(R.id.carNumber);
		customPhone=find(R.id.customPhone);
		startPoint=find(R.id.startPoint);
		endPoint=find(R.id.endPoint);
		distance=find(R.id.distance);
		chargeNum=find(R.id.chargeNum);
		
		intent=getIntent();
		historicalCase=(HistoricalCase) intent.getExtras().get("historicalCase");
		if(null==historicalCase.getTotal()||"".equals(historicalCase.getTotal())){
			chargeNum.setText(historicalCase.getPredictCharge());
		}else{
			chargeNum.setText(historicalCase.getTotal());
		}
		
		caseNumber.setText(historicalCase.getCaseNumber());
		carNumber.setText(historicalCase.getTargetCar());
		customPhone.setText(historicalCase.getReportPhone());
		startPoint.setText(historicalCase.getGrabPlace());
		endPoint.setText(historicalCase.getOccurPlace());
		distance.setText("距您"+historicalCase.getGrabDistance()+"km");

		caseEvaluation=historicalCase.getCaseEvaluationInfo();
		if(null==caseEvaluation||caseEvaluation.equals("")){
//			evaluates=new ArrayList<>();
			evaluates=getDefaultEvaluates();
		}else{
			evaluates=getEvaluates();
		}
		evaluate_list=find(R.id.evaluate_list);
		evaAdapter=new EvaluateListAdapter(evaluates, TaskDetailActivity.this);
		evaluate_list.setAdapter(evaAdapter);
		evaluate_list.setVisibility(View.GONE);
		
		chargeList=getChargeList();
		charge_list=find(R.id.charge_list);
		charAdapter=new ChargeListAdapter(chargeList, TaskDetailActivity.this);
		charge_list.setAdapter(charAdapter);
		charge_list.setVisibility(View.GONE);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.returnBack:
			finish();
			break;
			
		case R.id.evaluate:
			if(evaluateVisibal){
				evaluate_list.setVisibility(View.GONE);
				evaluateVisibal=false;
			}else{
				evaluate_list.setVisibility(View.VISIBLE);
				evaluateVisibal=true;
			}
			break;

		case R.id.totalCharge:
			if(chargeVisibal){
				charge_list.setVisibility(View.GONE);
				chargeVisibal=false;
			}else{
				charge_list.setVisibility(View.VISIBLE);
				chargeVisibal=true;
			}
			break;
			
		default:
			break;
		}
	}
	
	private List<Evaluation> getEvaluates(){
		evaluates=new ArrayList<>();
		Evaluation evaluate=new Evaluation();
		evaluate.setEvaluateName("查勘时效");
		if(null==caseEvaluation.getSurveyAgingScore()||caseEvaluation.getSurveyAgingScore().equals("")){
			evaluate.setEvaluateScore(0);
		}else{
			evaluate.setEvaluateScore(Integer.parseInt(caseEvaluation.getSurveyAgingScore()));
		}
		
		evaluates.add(evaluate);
		evaluate=new Evaluation();
		evaluate.setEvaluateName("查勘技能");
		if(null==caseEvaluation.getSurveySkillScore()||caseEvaluation.getSurveySkillScore().equals("")){
			evaluate.setEvaluateScore(0);
		}else{
			evaluate.setEvaluateScore(Integer.parseInt(caseEvaluation.getSurveySkillScore()));
		}
		
		evaluates.add(evaluate);
		evaluate=new Evaluation();
		evaluate.setEvaluateName("服务态度");
		if(null==caseEvaluation.getServiceAttitudeScore()||caseEvaluation.getServiceAttitudeScore().equals("")){
			evaluate.setEvaluateScore(0);
		}else{
			evaluate.setEvaluateScore(Integer.parseInt(caseEvaluation.getServiceAttitudeScore()));
		}
		
		evaluates.add(evaluate);
		evaluate=new Evaluation();
		evaluate.setEvaluateName("配合力度");
		if(null==caseEvaluation.getCooperateAbilityScore()||caseEvaluation.getCooperateAbilityScore().equals("")){
			evaluate.setEvaluateScore(0);
		}else{
			evaluate.setEvaluateScore(Integer.parseInt(caseEvaluation.getCooperateAbilityScore()));
		}
		
//		evaluates.add(evaluate);
//		evaluate=new Evaluation();
//		evaluate.setEvaluateName("客户满意度");
//		if(caseEvaluation.getClientSatisfactionScore().equals("")||caseEvaluation.getClientSatisfactionScore()==null){
//			evaluate.setEvaluateScore(0);
//		}else{
//			evaluate.setEvaluateScore(Integer.parseInt(caseEvaluation.getClientSatisfactionScore()));
//		}
		
		evaluates.add(evaluate);
		return evaluates;
	}
	
	private List<Evaluation> getDefaultEvaluates(){
		evaluates=new ArrayList<>();
		Evaluation evaluate=new Evaluation();
		evaluate.setEvaluateName("查勘时效");
		evaluate.setEvaluateScore(0);
		evaluates.add(evaluate);
		evaluate=new Evaluation();
		evaluate.setEvaluateName("查勘技能");
		evaluate.setEvaluateScore(0);
		evaluates.add(evaluate);
		evaluate=new Evaluation();
		evaluate.setEvaluateName("服务态度");
		evaluate.setEvaluateScore(0);
		evaluates.add(evaluate);
		evaluate=new Evaluation();
		evaluate.setEvaluateName("配合力度");
		evaluate.setEvaluateScore(0);
		evaluates.add(evaluate);
//		evaluate=new Evaluation();
//		evaluate.setEvaluateName("客户满意度");
//		evaluate.setEvaluateScore(0);
//		evaluates.add(evaluate);
		return evaluates;
	}
	
	private List<ChargeVO> getChargeList(){
		chargeList=new ArrayList<>();
		
		ChargeVO chargeVO=new ChargeVO();
		chargeVO.setName("查勘费");
		if(null==historicalCase.getSurveyCost()||historicalCase.getSurveyCost().equals("")){
			chargeVO.setCharge("");
		}else{
			chargeVO.setCharge(historicalCase.getSurveyCost());
		}
		chargeList.add(chargeVO);
		
		chargeVO=new ChargeVO();
		chargeVO.setName("查勘补贴");
		if(null==historicalCase.getPostAllowance()||historicalCase.getPostAllowance().equals("")){
			chargeVO.setCharge("");
		}else{
			chargeVO.setCharge(historicalCase.getPostAllowance());
		}
		chargeList.add(chargeVO);
		
		return chargeList;
	}
}
